package com.jelly.web.share.upload.controller;

import com.jelly.common.exception.UploadImgException;
import com.jelly.common.util.web.AjaxPostResponse;
import com.jelly.web.share.base.controller.BaseController;
import com.jelly.web.share.upload.service.IUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @创建人 ql
 * @创建时间 2018-10-18 09:37
 * @项目名称 jelly
 * @功能描述: 上传控制器
 **/
@Controller
@RequestMapping("/upload")
public class UploadController extends BaseController {

    @Autowired
    private IUploadService uploadService;

    /**
     * 上传图片
     * @param file
     * @param request
     * @param type
     * @return
     * @throws IOException
     */
    @PostMapping("/image")
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> image(@RequestParam("file") MultipartFile file, HttpServletRequest request,@RequestParam String type)throws IOException {

        try{
            return okResponse(uploadService.upload(file,type));

        }catch (UploadImgException e){
            return errorResponse(e.getMessage());
        }catch (Exception e){
            return errorResponse("上传失败请联系管理员");
        }

    }

}
